from django.shortcuts import *
from .models import *
from django.http import *

# Create your views here.
def index(request):
    return render(request, 'index.html')


def login(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    # else:
    #     uname = request.POST.get('uname')
    #     pwd = request.POST.get('pwd')
    #     user = User.objects.values('uname').filter(uname=uname, pwd=pwd)
    #     if user:
    #         for i in user:
    #            v =  i['uname']
    #            request.session['name']=v
    #         return redirect(reverse('all'))
    #     else:
    #         # return HttpResponse('hh')
    #         return JsonResponse({'flag':1})


def all(request):
    alls = Info.objects.all()
    return render(request,'all.html',{'alls':alls})


def add(request):
    if request.method == 'GET':
        ll = Type.objects.all()
        return render(request,'add.html',{'ll':ll})
    else:
        tit = request.POST.get('tit')
        ath = request.POST.get('ath')
        cont = request.POST.get('cont')
        ss = request.POST.get('ss')
        type = request.POST.get('type')
        new = Info(tit=tit,ath=ath,cont=cont,ss=ss,Type_id=type)
        new.save()
        return redirect(reverse('all'))


def update(request,id,sid):
    if request.method == 'GET':
        all = Info.objects.filter(pk=id).first()
        ll = Type.objects.all()
        return render(request,'update.html',{'all':all,'ll':ll})
    else:
        tit = request.POST.get('tit')
        ath = request.POST.get('ath')
        cont = request.POST.get('cont')
        ss = request.POST.get('ss')
        type = request.POST.get('type')
        Info.objects.filter(pk=id).update(tit=tit,ath=ath,cont=cont,ss=ss,Type_id=type)
        return redirect(reverse('all'))
def delete(request,id):
    all = Info.objects.filter(pk=id)
    all.delete()
    return redirect(reverse('all'))


def loginoy(request):
    if request.method == 'POST':
        uname = request.POST.get('name')
        pwd = request.POST.get('pwd')
        user = User.objects.values('uname').filter(uname=uname, pwd=pwd)
        if user:
            for i in user:
               v =  i['uname']
               request.session['name']=v
            return JsonResponse({'flag':1})
        else:
            # return HttpResponse('hh')
            return JsonResponse({'flag':0})